package cn.frh.basic.controller;

import cn.frh.basic.util.AjaxResult;
import cn.frh.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/fastDfs")
@RestController
public class FastDfsController {

    @PostMapping
    public AjaxResult upload(@RequestPart(required = true, value = "file") MultipartFile file) {
        try {
            //获取后缀
            String filename = file.getOriginalFilename();
            String ext = filename.substring(filename.lastIndexOf(".") + 1);
            String upload = FastDfsUtil.upload(file.getBytes(), ext);
            return AjaxResult.me().setResultObj(upload);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞，系统错误");
        }
    }

    @DeleteMapping
    public AjaxResult delete(@RequestParam String path) {
        try {
            String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            String fileName = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
            FastDfsUtil.delete(groupName,fileName);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞，删除失败");
        }
    }
}
